<?php

class Poll_AdminPollController extends Core_Controller_Action_Admin {

    public function indexAction() {

        // navigation
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
                ->getNavigation('poll_admin_main', array (), 'poll_admin_main_poll');


        $page = $this->_getParam('page', 1);
        $values = $this->_getAllParams();
        $params=array();
        $params['orderby']="creation_date DESC";
        $params["order_direction"]="DESC";
        $values = array_merge($params, $values);
        
        if ($this->getRequest()->isPost()) {
            $this->view->search_title = $params['title'] = $this->_getParam('title');
            $this->view->search_owner = $params['owner'] = $this->_getParam('owner');
            $this->view->search_privacy = $params['privacy'] = $this->_getParam('privacy');
            $this->view->search_feature = $params['feature'] = $this->_getParam('feature');
        }
        
        // Make paginator
        $paginator = Engine_Api::_()->getApi('Poll', 'Poll')
                ->getPollsPaginator($values);

        $paginator->setCurrentPageNumber($page);
        $pageNumber = Engine_Api::_()->getApi('settings', 'core')
                ->getSetting('poll.autionperpage.defautvalue');
        $paginator->setItemCountPerPage($pageNumber);

        $this->view->paginator = $paginator;
        $this->view->formValues = array_filter($values);
    }

    public  function deleteAction() {

        $poll_id=$this->_getParam('id');
        $user_id = $this->_getParam('user_id');
        $poll = Engine_Api::_()->getDbTable('Polls', 'Poll')->find($poll_id)->current();
        $poll_option = Engine_Api::_()->getDbTable('Options', 'Poll')->find($poll_id)->current();


        $form = $this->view->form = new Poll_Form_Admin_Poll_Delete();

        if( $this->getRequest()->isPost() ) {
            $db = Engine_Db_Table::getDefaultAdapter();
            $db->beginTransaction();
            try {
                //delete all deal
                $poll->delete();
                $table = Engine_Api::_()->getDbtable('options', 'poll');
                $table1 = Engine_Api::_()->getDbtable('votes', 'poll');
                $db->delete($table->info('name'), array(
                        'poll_id = ?'=> $poll_id
                ));
                //$questionpoll_vote->delete();
                $db->commit();
            }
            catch( Exception $e ) {
                $db->rollBack();
                throw $e;
            }
            $this->_forward('success', 'utility', 'core', array(
                    'parentRefresh'=>10,
                    'messages' => array("Poll had delete")
            ));
        }
    }
    public function multimodifyAction() {
        if( $this->getRequest()->isPost() ) {
            $qids = $this->_getParam('qid');
            if (count($qids)) {
                try {
                    $db = Engine_Db_Table::getDefaultAdapter();
                    $db->beginTransaction();
                    foreach ($qids as $qid) {
                        $question = Engine_Api::_()->getDbTable('Polls', 'Poll')->find($qid)->current(); 
                        if ($question) {
                            $voteTb = Engine_Api::_()->getDbTable('Votes', 'Poll'); 
                            $options = Engine_Api::_()->getDbTable('Options', 'Poll')->fetchAll("Poll_id = {$question->getIdentity()}");           
                            
                            foreach ($options as $option) {
                                Engine_Api::_()->getDbTable('Votes', 'Poll')->delete("option_id = {$option->getIdentity()}");
                                $option->delete();    
                            }
                            
                            $attachments = Engine_Api::_()->getDbTable('Attachments', 'Activity')->fetchAll("type = '{$question->getType()}' AND id = {$question->getIdentity()}");
                            if ($attachments) {
                                foreach ($attachments as $attachment) {
                                    $action = Engine_Api::_()->getDbTable('Actions', 'Activity')->fetchRow("action_id = {$attachment->action_id}");
                                    $action->deleteItem();
                                    $attachment->delete();
                                }
                            }
                            
                            $question->delete(); 
                        }
                    }
                    $db->commit();
                }
                catch( Exception $e ) {
                    $db->rollBack();
                    throw $e;
                }    
            }
            
        }
        return $this->_helper->redirector->gotoRoute(array('action' => 'index'));
    }
    
    public function featureAction() {
        $question = Engine_Api::_()->getDbTable('Polls', 'Poll')->find($this->_getParam('id'))->current();
        if ($question) { 
            $question->feature = $question->feature ? 0 : 1;
            $question->save();
        }
        $this->_helper->redirector->gotoRoute(array('action' => 'index', 'controller' => 'poll', 'module' => 'poll'), 'admin_default', true);
    }
}